#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
'''
@File    :   main_project.py
@Time    :   2022/02/02 15:26:07
@Author  :   chou 
@Version :   1.0
@Contact :   chou2079986882@gmail.com
@Desc    :   None
'''


import pymysql
import requests
import sys
import time
import json
import pandas as pd
from selenium import webdriver
from hashlib import md5

# 请求头和url信息
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36"}
url1 = ' https://wxyqfk.zhxy.net/?yxdm=10623#/login'
url2 = 'https://wxyqfk.zhxy.net/?yxdm=10623#/clockIn'


# requests登录
def login(stu_name,stu_password,stu_xgh):
    data_login = {
        "Name": stu_name,
        "PassWord": stu_password,
        "UserType": "1",
        "XGH": stu_xgh,
        "YXDM": "10623"
    }
    login_url = "https://wxyqfk.zhxy.net/?yxdm=10623#/login"
    # 登陆
    s = requests.Session()
    s.post(login_url, data=data_login, headers=headers)
    # post登陆页面
    login_result = s.post("https://yqfkapi.zhxy.net/api/User/CheckUser")
    # print(login_result)
    return s


# requests post数据部分
def health_daka(stu_uid,JWD,Place,s, key, code):
    data_health = {
        "UID": stu_uid,
        "UserType": "1",
        "JWD": JWD,
        "key": key,
        "code": code,
        "ZZDKID": "37",
        "A1": "正常",
        "A4": "无",
        "A2": "全部正常",
        "A3": Place,
        "A11": "在校",
        "A12": "未实习",
        "A13": "低风险区",
        "YXDM": "10623",
        "version": "v1.3.2"
    }

    # for i in range(2):
    health_url = 'https://wxyqfk.zhxy.net/?yxdm=10623#/clockIn'
    s.post(health_url, headers=headers)
    health_result = s.post('https://yqfkapi.zhxy.net/api/ClockIn/Save', data=data_health, headers=headers)


# 验证码
def new_ocr():
    pic1 = requests.get('https://yqfkapi.zhxy.net/api/common/getverifycode')
    tex1 = pic1.content
    tex2 = bytes.decode(tex1)
    if json.loads(tex2)['info'] == '非法访问！':
        print(tex2)
        sys.exit(1)
    tex3 = json.loads(tex2)['data']['img']
    key = json.loads(tex2)['data']['key']
    url = 'data:image/png;base64,' + tex3
    # print(url)
    # print(qr_key)
    # 1.识别验证码
    img_url = url
    from ddddocr_NB import free
    from urllib.request import urlretrieve
    urlretrieve(img_url, './qrcode_temp.png')
    filepath = "./qrcode_temp.png"
    code = free(filepath)
    # code="1234"
    print(code)
    print("调用了免费接口（ddddocr接口）")
    return key, code



# 获取uid
def uid(stu_xgh,stu_name,stu_password):
    formdata = {
        "YXDM": "10623",
        "UserType": "1",
        "XGH": stu_xgh,
        "Name": stu_name,
        "PassWord": stu_password
    }
    url = "https://yqfkapi.zhxy.net/api/User/CheckUser"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
    response = requests.post(url, data=formdata, headers=headers)
    # 使用json()方法，将response对象，转为列表/字典
    json_response = response.json()
    # print(json_response)
    # 获取UID
    UID = json_response['data']['ID']
    # print(UID)
    stu_uid = str(UID)
    return stu_uid


# 连接数据库获取信息,sql_query可以改变获取到的数据库，方便后面检测调用
#sql_query = 'SELECT * FROM user'表示所有人信息
#sql_query = 'SELECT * FROM fall'错误人员的信息
def sql_data(sql_query):
    # 1.连接
    conn = pymysql.connect(host='localhost',user='daka', password='1234c', db='daka')
    table = pd.read_sql(sql_query, con=conn)
    conn.close()
    # print(df)
    return table


# 插入数据
def sql_insert(sql_query):
    # 1.连接
    conn = pymysql.connect(host='localhost',user='daka', password='1234c', db='daka')
    if conn:
        print("连接成功")
    else:
        print('连接失败')
    # 2.创建游标
    cursor = conn.cursor()
    # 使用 execute() 方法执行 SQL，如果表存在则删除
    cursor.execute(sql_query)
    cursor.close()
    print("执行完成")


import requests
import time
def push(text):
    key="PDU3765TBs56dzcIw5d6WcV4Qe7qqo9MNTdREyuB"
    # text="无内容"
    url=f"https://api2.pushdeer.com/message/push?pushkey={key}&text={text}"
    requests.get(url)
    